/* 
 * cms，一个基于J2EE架构内容管理系统
 * Copyright © GrayRabbit Co., Ltd. All rights reserved.
 * Department:运营department
 * 更多信息请访问：
 * http://code.google.com/p/gray-rabbit-cms/
 */
package org.grayrabbit.cms.action.search;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.grayrabbit.cms.action.common.BaseAction;
import org.grayrabbit.cms.constant.ConstantEnum.HotKeywordType;
import org.grayrabbit.cms.constant.ConstantEnum.Module;
import org.grayrabbit.cms.constant.ConstantEnum.OperAct;
import org.grayrabbit.cms.entity.HotKeyword;
import org.grayrabbit.cms.service.search.HotKeywordService;
import org.grayrabbit.cms.vo.search.HotKeywordBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import org.grayrabbit.dao.util.Page;

/**
 * <b>HotKeywordAction</b> Struts2 action�?





 * @version 1.0,创建时间:2011-05-16 14:51:27 
 */
@Controller
@Scope("prototype")
@Results({
		@Result(name = "reload", type = "redirectAction", location = "hotkeyword-list"),
		@Result(name = "success", type = "dispatcher", location = "hotkeyword-list.jsp"),
		@Result(name = "input", type = "dispatcher", location = "hotkeyword-input.jsp")
		})
public class HotKeywordAction extends BaseAction<HotKeywordBean> {
	private static final long serialVersionUID = -1L;
	//Service接口接入
	@Autowired
	@Qualifier("hotKeywordServiceImpl")
	private HotKeywordService hotKeywordService;
	
	//VO对象，由前端页面数据组装
	private HotKeywordBean hotKeyword;
	private HotKeywordType[] hotKeywordTypes =HotKeywordType.values();
	
	/**return 前端页面数据组装VO对象,<b>HotKeywordBean</b>*/
	public HotKeywordBean getHotKeyword() {
		return hotKeyword;
	}

	public void setHotKeyword(HotKeywordBean hotKeyword) {
		this.hotKeyword = hotKeyword;
	}
	private String orig;
	private String ids;
	
	//分页对象
	private Page<HotKeyword> page = new Page<HotKeyword>(MAX_PAGE_NUM,true);
	
	/*
	 * return分页对象，默认实例化。如：page.pageNu=2,将更新相应的对象property
	 */
	public Page<HotKeyword> getPage() {
		return page;
	}
	//接收前端form,url提交参数组装成分页property
	public void setPage(Page<HotKeyword> page) {
		this.page = page;
	}
	
	/**
	 * 在执行（struts2的action调用时）list()方法前执行本方法
	 */
	public void prepareList(){
		prepareModel();
	}
	
	/**
	 * 在执行任何其它action方法（struts2的action调用时）前执行本方法
	 */
	public void prepare(){
		if(hotKeyword==null){
			hotKeyword = new HotKeywordBean();
		}
	}
	/**
	 * 实现ModelDriven的接口简例前端页的bean组装写法,
	 *  如：
	 *	<pre>
	 *		<code> name="user.userName"</code>
	 *	改写成：
	 *		<code> name="userName"</code>
	 *	action实例中，会自动组成user成员变量
	 *	</pre>
	 */
	public HotKeywordBean getModel() {
		return hotKeyword;
	}
	
	@Override
	protected void prepareModel(){
		if(hotKeyword != null && StringUtils.isNotBlank(hotKeyword.getHotKeywordId())){
			hotKeyword = (HotKeywordBean)dozer.map(hotKeywordService.getHotKeyword(hotKeyword.getHotKeywordId()),HotKeywordBean.class);
		}
		
	}
	/**
	 * 批量id处理
	 * @return
	 */
	private String[] getBatchIds(){
		String[] batchIds = null;
		if((null != ids) && (StringUtils.isNotBlank(ids))){
			if(ids.startsWith("on@")){
				String allId = ids.substring(ids.indexOf("@")+1);
				batchIds=allId.split("@");
			}else{
				batchIds=ids.split("@");
			}
		}
		return batchIds;
	}

	/**
	 * 删除<b>HotKeyword</b>记录的方法，成员变量<b>key</b>为主键的String数组，以主键方式进行删作
	 * 删除成功后，返回"reload"，然后跳转到hotkeyword-list.action
	 */
	@Override
	@Action(value="hotkeyword-delete")
	public String delete(){
		String[] batchIds = getBatchIds();
		if(batchIds != null){
			hotKeywordService.delete(batchIds);
			for(int i = 0; i < batchIds.length; i++){
				addLog(Module.SYSTEM.getValue(), OperAct.DEL.getValue(), batchIds[i]);
			}
		}else{
			hotKeywordService.delete(key);
			addLog(Module.SYSTEM.getValue(), OperAct.DEL.getValue(), key[0]);
		}
		return RELOAD;
	}
	
	/**
	 * HotKeyword的列表方法，通过成员变量<b>page�?{insClassName}</b>调用service接口，更新page
	 * 执行成功后，将映射到hotkeyword-list.jsp页面
	 */
	@Override
	@Action(value="hotkeyword-list")
	public String list(){
		hotKeywordService.list(page,hotKeyword);
		return SUCCESS;
	}
	public String transForm(String hotType) {
		String str = "";
		for(HotKeywordType type:  hotKeywordTypes){
			if(type.getKey().equals(hotType)){
				str += type.getValue();
			}
		}		
		return str;
	}

	/**
	 * HotKeyword的新增与编辑的方法执行成功能
	 * 将映射到hotkeyword-input.jsp
	 */
	@Action(value="hotkeyword-input")
	public String input(){
		prepareModel();
		return INPUT;
	}
	
	/**
	 * save or update HotKeyword的方法，将成员property hotKeyword转成HotKeyword实例，执行保存；
	 * 执行成功后，返回"reload"，然后跳转到hotkeyword-list.action
	 */
	@Override
	@Action(value="hotkeyword-save")
	public String save(){
		HotKeyword obj = (HotKeyword) dozer.map(hotKeyword, HotKeyword.class);
		Map<String, String> map = new HashMap<String, String>();
		if(!StringUtils.isNotBlank(obj.getHotKeywordId())){
			obj.setHotKeywordId(null);
			obj.setIsGenerate(false);
			obj.setSearchNum(0L);
		}
		if(obj != null){
			String[] hotKeywords = obj.getHotKeywordName().trim().split(",");
			String[] frontFilenames = obj.getFrontFilename().trim().split(",");
			for(int i = 0; i < hotKeywords.length; i++){
				if(StringUtils.isNotBlank(hotKeywords[i])){
					if(!map.containsKey(hotKeywords[i].trim())){
						map.put(hotKeywords[i].trim(), frontFilenames[i].trim());
					}
				}
			}
			String s = map.toString();
			String[] arr = s.substring(1, s.length()-1).split(",");
				for(int i=0;i<arr.length;i++){
					String[] arr2 = arr[i].split("=");
					if(!ArrayUtils.isEmpty(arr2)){
						HotKeyword hk = new HotKeyword();
						hk.setHotKeywordId(obj.getHotKeywordId());
						hk.setIsGenerate(obj.getIsGenerate());
						hk.setSearchNum(obj.getSearchNum());
						hk.setHotKeywordName(arr2[0].trim());
						hk.setFrontFilename(arr2[1].trim());
						hk.setHotType(HotKeywordType.CUSTOM.getKey());
						
						hk.setFirstTime(new Date());
						hotKeywordService.save(hk);
						if(StringUtils.isNotBlank(obj.getHotKeywordId())){
							addLog(Module.SYSTEM.getValue(), OperAct.EDIT.getValue(), hotKeyword.getHotKeywordId());
						}else{
							addLog(Module.SYSTEM.getValue(), OperAct.ADD.getValue(), obj.getHotKeywordId());
						}
					}
				}
		}
		return RELOAD;
	}
	
	@Action(value="hotkeyword-checkkeyword")
	public String checkKeyword(){
		String text = "";
		
		if(StringUtils.isBlank(hotKeyword.getHotKeywordName())){
			Boolean frontFilenameUnique = hotKeywordService.checkFilenameUnique(hotKeyword.getFrontFilename(),hotKeyword.getHotKeywordId());
			if(frontFilenameUnique){
				text="frontFilenameNo";
			}else{
				text="frontFilenameYes";
			}
		}
		if(StringUtils.isBlank(hotKeyword.getFrontFilename())){
			Boolean keywordUnique = hotKeywordService.checkUnique(hotKeyword.getHotKeywordName(),hotKeyword.getHotKeywordId());
			if(keywordUnique){
				text = "keywordNo";
			}else{
				text = "keywordYes";
			}
		}
		return renderText(text);
	}

	public HotKeywordType[] getHotKeywordTypes() {
		return hotKeywordTypes;
	}

	public void setHotKeywordTypes(HotKeywordType[] hotKeywordTypes) {
		this.hotKeywordTypes = hotKeywordTypes;
	}
	public String getOrig() {
		return orig;
	}

	public void setOrig(String orig) {
		this.orig = orig;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}
	
}